﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using PointOfSale.Global;
using PointOfSale.PointOfSale;
using PointOfSale.Class;
using PointOfSale.Report.Sale;

namespace PointOfSale.Transaction
{
	/// <summary>
	/// Summary description for POSTransactionsList.
	/// </summary>
	public class frmPOSTransactionsList : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Button btnPrint;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.ListView listView1;

		private ListView.SelectedListViewItemCollection selectedRow = null;
		private System.Data.DataSet dataSet = null;
		private System.Data.SqlClient.SqlCommand sqlCommand = null;
		private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter = null;
		private System.Windows.Forms.Button btnChangeDetail;
		private System.Windows.Forms.TextBox txtSoHoaDon;
		private System.Windows.Forms.Label lblSoHoaDon;
		private System.Windows.Forms.ColumnHeader colSaleID;
		private System.Windows.Forms.ColumnHeader colBillDate;
		private System.Windows.Forms.ColumnHeader colTotal;
		private System.Windows.Forms.ColumnHeader colBillAmount;
		private System.Windows.Forms.ColumnHeader colPaidAmount;
		private System.Windows.Forms.ColumnHeader colPostAmount;
		private System.Windows.Forms.ColumnHeader colUserId;
		private System.Windows.Forms.ColumnHeader colRemark;
		private System.Windows.Forms.ColumnHeader colRoomService;
		private System.Windows.Forms.DateTimePicker dtpngay;
		private System.Windows.Forms.ColumnHeader colSoHoaDon;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmPOSTransactionsList()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.dtpngay = new System.Windows.Forms.DateTimePicker();
			this.label4 = new System.Windows.Forms.Label();
			this.lblSoHoaDon = new System.Windows.Forms.Label();
			this.txtSoHoaDon = new System.Windows.Forms.TextBox();
			this.btnClose = new System.Windows.Forms.Button();
			this.btnPrint = new System.Windows.Forms.Button();
			this.listView1 = new System.Windows.Forms.ListView();
			this.colSoHoaDon = new System.Windows.Forms.ColumnHeader();
			this.colBillDate = new System.Windows.Forms.ColumnHeader();
			this.colTotal = new System.Windows.Forms.ColumnHeader();
			this.colRoomService = new System.Windows.Forms.ColumnHeader();
			this.colBillAmount = new System.Windows.Forms.ColumnHeader();
			this.colPaidAmount = new System.Windows.Forms.ColumnHeader();
			this.colPostAmount = new System.Windows.Forms.ColumnHeader();
			this.colUserId = new System.Windows.Forms.ColumnHeader();
			this.colRemark = new System.Windows.Forms.ColumnHeader();
			this.colSaleID = new System.Windows.Forms.ColumnHeader();
			this.btnChangeDetail = new System.Windows.Forms.Button();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.dtpngay);
			this.groupBox3.Controls.Add(this.label4);
			this.groupBox3.Controls.Add(this.lblSoHoaDon);
			this.groupBox3.Controls.Add(this.txtSoHoaDon);
			this.groupBox3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox3.Location = new System.Drawing.Point(72, 8);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(552, 48);
			this.groupBox3.TabIndex = 2;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Chọn thông tin tìm kiếm";
			// 
			// dtpngay
			// 
			this.dtpngay.Location = new System.Drawing.Point(184, 16);
			this.dtpngay.Name = "dtpngay";
			this.dtpngay.Size = new System.Drawing.Size(88, 20);
			this.dtpngay.TabIndex = 9;
			this.dtpngay.ValueChanged += new System.EventHandler(this.dtpngay_ValueChanged);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(120, 16);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 16);
			this.label4.TabIndex = 8;
			this.label4.Text = "Ngày lập";
			// 
			// lblSoHoaDon
			// 
			this.lblSoHoaDon.AutoSize = true;
			this.lblSoHoaDon.Location = new System.Drawing.Point(296, 16);
			this.lblSoHoaDon.Name = "lblSoHoaDon";
			this.lblSoHoaDon.Size = new System.Drawing.Size(62, 16);
			this.lblSoHoaDon.TabIndex = 14;
			this.lblSoHoaDon.Text = "Số hóa đơn";
			// 
			// txtSoHoaDon
			// 
			this.txtSoHoaDon.Location = new System.Drawing.Point(376, 16);
			this.txtSoHoaDon.Name = "txtSoHoaDon";
			this.txtSoHoaDon.Size = new System.Drawing.Size(88, 20);
			this.txtSoHoaDon.TabIndex = 13;
			this.txtSoHoaDon.Text = "";
			this.txtSoHoaDon.TextChanged += new System.EventHandler(this.txtSoHoaDon_TextChanged);
			// 
			// btnClose
			// 
			this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnClose.Location = new System.Drawing.Point(664, 528);
			this.btnClose.Name = "btnClose";
			this.btnClose.TabIndex = 6;
			this.btnClose.Text = "Thoát";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnPrint
			// 
			this.btnPrint.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnPrint.Location = new System.Drawing.Point(584, 528);
			this.btnPrint.Name = "btnPrint";
			this.btnPrint.TabIndex = 7;
			this.btnPrint.Text = "In hóa đơn";
			this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
			// 
			// listView1
			// 
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.colSoHoaDon,
																						this.colBillDate,
																						this.colTotal,
																						this.colRoomService,
																						this.colBillAmount,
																						this.colPaidAmount,
																						this.colPostAmount,
																						this.colUserId,
																						this.colRemark,
																						this.colSaleID});
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.Location = new System.Drawing.Point(8, 56);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(728, 464);
			this.listView1.TabIndex = 10;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseUp);
			// 
			// colSoHoaDon
			// 
			this.colSoHoaDon.Text = "Số hóa đơn";
			this.colSoHoaDon.Width = 71;
			// 
			// colBillDate
			// 
			this.colBillDate.Text = "Ngày lập";
			// 
			// colTotal
			// 
			this.colTotal.Text = "Tổng tiền";
			this.colTotal.Width = 73;
			// 
			// colRoomService
			// 
			this.colRoomService.Text = "Các loại phí khác";
			this.colRoomService.Width = 103;
			// 
			// colBillAmount
			// 
			this.colBillAmount.Text = "Tổng hóa đơn";
			this.colBillAmount.Width = 90;
			// 
			// colPaidAmount
			// 
			this.colPaidAmount.Text = "Đã trả";
			this.colPaidAmount.Width = 0;
			// 
			// colPostAmount
			// 
			this.colPostAmount.Text = "Còn nợ";
			this.colPostAmount.Width = 0;
			// 
			// colUserId
			// 
			this.colUserId.Text = "Người lập";
			this.colUserId.Width = 83;
			// 
			// colRemark
			// 
			this.colRemark.Text = "Ghi chú";
			this.colRemark.Width = 245;
			// 
			// colSaleID
			// 
			this.colSaleID.Text = "saleId";
			this.colSaleID.Width = 0;
			// 
			// btnChangeDetail
			// 
			this.btnChangeDetail.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnChangeDetail.Location = new System.Drawing.Point(496, 528);
			this.btnChangeDetail.Name = "btnChangeDetail";
			this.btnChangeDetail.Size = new System.Drawing.Size(80, 23);
			this.btnChangeDetail.TabIndex = 11;
			this.btnChangeDetail.Text = "Sửa hóa đơn";
			this.btnChangeDetail.Click += new System.EventHandler(this.btnChangeDetail_Click);
			// 
			// frmPOSTransactionsList
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(742, 555);
			this.Controls.Add(this.btnChangeDetail);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.btnPrint);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.groupBox3);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.KeyPreview = true;
			this.Name = "frmPOSTransactionsList";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Danh sách hóa đơn bán hàng";
			this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmPOSTransactionsList_KeyDown);
			this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.frmPOSTransactionsList_KeyPress);
			this.Load += new System.EventHandler(this.frmPOSTransactionsList_Load);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void frmPOSTransactionsList_Load(object sender, System.EventArgs e)
		{
			//Tao cac item cho cboYear
			this.dtpngay.Value =DateTime.Now.Date;
			this.btnPrint.Enabled = false;

			this.CancelButton = this.btnClose;

			this.sqlCommand = new SqlCommand();
			this.sqlCommand.Connection = clsGlobal.g_DataConn;

			this.sqlDataAdapter = new SqlDataAdapter();
			this.sqlDataAdapter.SelectCommand = this.sqlCommand;

			this.dataSet = new DataSet();
			this.dataSet.CaseSensitive = true;

			this.txtSoHoaDon.Focus();
			this.CancelButton = this.btnClose;
			hienlist();
//			string strSql = "select a.saleId, a.invoiceDate, a.total, a.invoiceAmount, a.paidAmount, a.postAmount, a.userId, a.roomServiceTax, a.remark, a.departmentId, b.userFullName,a.guestId, a.SoHoaDon  from tblSale a "
//				+ " inner join tblUser b on a.userId = b.userId ";
//			strSql += "where datediff(day,a.Invoicedate,'"+this.dtpngay.Value.ToString("MMM dd yyyy")+"') = 0";
//			string selectCommand = strSql;
//			DataTable dt = clsSale.GetBillList(strSql);
//			for(int i = 0; i < dt.Rows.Count; i++)
//			{
//				ListViewItem lvi = new ListViewItem(new string[]{dt.Rows[i]["SoHoaDon"].ToString(), dt.Rows[i]["invoiceDate"].ToString(), 
//																		dt.Rows[i]["total"].ToString(), dt.Rows[i]["roomServiceTax"].ToString(), 
//																		dt.Rows[i]["invoiceAmount"].ToString(), dt.Rows[i]["paidAmount"].ToString(), 
//																		dt.Rows[i]["postAmount"].ToString(), dt.Rows[i]["userFullName"].ToString(), 
//																		(dt.Rows[i]["GuestId"].ToString() != "-1") ? "Phieu ky da chuyen":dt.Rows[i]["remark"].ToString(), dt.Rows[i]["saleId"].ToString()});	
//					this.listView1.Items.Add(lvi);
//			}
		}

				
		private void hienlist()
		{
			//Xoa cac item hien co trong listview
			foreach(ListViewItem item in this.listView1.Items)
			{
				this.listView1.Items.Remove(item);
			}
			
			string strSql = "select a.saleId, a.invoiceDate, a.total, a.invoiceAmount, a.paidAmount, a.postAmount, a.userId, a.roomServiceTax, a.remark, a.departmentId, b.userFullName,a.guestId, a.SoHoaDon  from tblSale a "
						+ " inner join tblUser b on a.userId = b.userId ";

			if(this.txtSoHoaDon.Text.Length == 0)
			{
				strSql += "where datediff(day,a.Invoicedate,'"+this.dtpngay.Value.ToString("MMM dd yyyy")+"') =0";
			}
			else
			{
				strSql += " where a.SoHoaDon = " + this.txtSoHoaDon.Text ;
			}
			
			string selectCommand = strSql;
			DataTable dt = clsSale.GetBillList(strSql);
			for(int i = 0; i < dt.Rows.Count; i++)
			{
				if(Convert.ToInt32(dt.Rows[i]["departmentId"]) == clsGlobal.g_DepartmentId)
				{
					ListViewItem lvi = new ListViewItem(new string[]{dt.Rows[i]["SoHoaDon"].ToString(), dt.Rows[i]["invoiceDate"].ToString(), 
																		dt.Rows[i]["total"].ToString(), dt.Rows[i]["roomServiceTax"].ToString(), 
																		dt.Rows[i]["invoiceAmount"].ToString(), dt.Rows[i]["paidAmount"].ToString(), 
																		dt.Rows[i]["postAmount"].ToString(), dt.Rows[i]["userFullName"].ToString(), 
																		(dt.Rows[i]["GuestId"].ToString() != "-1") ? "Phieu ky da chuyen":dt.Rows[i]["remark"].ToString(), dt.Rows[i]["saleId"].ToString()});	
					this.listView1.Items.Add(lvi);
				}
			}
		}

		private void linklblSelectAll_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
			this.ActiveControl = this.listView1;
			foreach(ListViewItem item in this.listView1.Items)
			{
				item.Selected = true;
			}

			this.selectedRow = this.listView1.SelectedItems;

			if(this.selectedRow.Count != 0)
			{
				this.btnPrint.Enabled = true;
			}
		}

		private void linklblUnSelectAll_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
			this.ActiveControl = this.listView1;
			foreach(ListViewItem item in this.listView1.Items)
			{
				item.Selected = false;
			}

			this.selectedRow = null;

			this.btnPrint.Enabled = false;
		}

//		private void btnDelete_Click(object sender, System.EventArgs e)
//		{
//			foreach(ListViewItem item in this.selectedRow)
//			{
//				this.listView1.Items.Remove(item);
//
//				this.sqlCommand.CommandText = "delete tblSaleDetail where saleID = " + item.SubItems[9].Text;
//				this.sqlDataAdapter.Fill(this.dataSet);
//				this.dataSet.Clear();
//
//				this.sqlCommand.CommandText = "delete tblSale where saleID = " + item.SubItems[9].Text;
//				this.sqlDataAdapter.Fill(this.dataSet);
//				this.dataSet.Clear();
//			}
//
//			this.btnPrint.Enabled = false;
//		}

		private void listView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			this.selectedRow = this.listView1.SelectedItems;

			if(this.selectedRow.Count != 0)
			{
				this.btnPrint.Enabled = true;
			}
			else
			{
				this.btnPrint.Enabled = false;
			}
		}

		private void btnEdit_Click(object sender, System.EventArgs e)
		{
		
		}

		private void btnPrint_Click(object sender, System.EventArgs e)
		{
			if(this.listView1.FocusedItem == null)
			{
				MessageBox.Show("Hãy chọn một hóa đơn để sửa. ");
			}
			else
			{
				int saleId = Convert.ToInt32(this.listView1.FocusedItem.SubItems[9].Text);
				frmInvoice _frmInvoice = new frmInvoice(saleId);
				_frmInvoice.Address = "Địa chỉ: " + clsGlobal.g_Hashtable["CompanyAddress"].ToString();
				_frmInvoice.Phone = "Điện thoại: " + clsGlobal.g_Hashtable["CompanyTel"].ToString();
				_frmInvoice.Fax = "Fax: " + clsGlobal.g_Hashtable["CompanyFax"].ToString();

				DataTable dt = clsSale.GetAnItem(saleId);

				_frmInvoice.service = Convert.ToDouble(dt.Rows[0]["roomServiceTax"]);
				_frmInvoice.amt = Convert.ToDouble(dt.Rows[0]["total"]);
				
				if(clsGlobal.g_UserFullName != "")
				{
					_frmInvoice.name = clsGlobal.g_UserFullName;
				}
				else
				{
					_frmInvoice.name = clsGlobal.g_UserName;
				}
				_frmInvoice.Show();
			}
		}

		private void btnChangeDetail_Click(object sender, System.EventArgs e)
		{
			if(this.listView1.FocusedItem != null)
			{
				int saleID = Convert.ToInt32(this.listView1.FocusedItem.SubItems[9].Text);
				DataTable dt = clsSale.GetAnItem(saleID);
				if(dt.Rows.Count == 0)
				{
					MessageBox.Show("Hóa đơn không tồn tại trong cơ sở dữ liệu. ");
				}
				else
				{
					int guestId = Convert.ToInt32(dt.Rows[0]["guestId"]);
					if(guestId != -1)
					{
						MessageBox.Show("Hóa đơn là phiếu ký đã chuyển sang lễ tân không sửa được. ");
					}
					else
					{
						frmEditSaleBill fpt = new frmEditSaleBill(saleID);
						fpt.Show();					
					}
				}
				
			}
			else
			{
				MessageBox.Show("Hãy chọn một hóa đơn để sửa. ");
			}
		}

		private void frmPOSTransactionsList_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
		}

		private void frmPOSTransactionsList_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
		
		}

		private void dtpngay_ValueChanged(object sender, System.EventArgs e)
		{
			hienlist();
		}

		private void txtSoHoaDon_TextChanged(object sender, System.EventArgs e)
		{
			hienlist();
		}
	}
}
